In [198]:
import networkx as nx
import matplotlib.pyplot as plt
import nxviz as nv

In [199]:
G = nx.barbell_graph(m1=8, m2=1)

In [200]:
nx.draw(G)
plt.show()


Degree Centrality

  • highly connected nodes

In [201]:
deg_cent = nx.degree_centrality(G)
deg_cent


Out[201]:
{0: 0.4375,
 1: 0.4375,
 2: 0.4375,
 3: 0.4375,
 4: 0.4375,
 5: 0.4375,
 6: 0.4375,
 7: 0.5,
 8: 0.125,
 9: 0.5,
 10: 0.4375,
 11: 0.4375,
 12: 0.4375,
 13: 0.4375,
 14: 0.4375,
 15: 0.4375,
 16: 0.4375}

In [202]:
for n in G.nodes():
    node = G.node[n]
    node['degree'] = nx.degree(G,n)
    node['dc'] = deg_cent[n]

In [203]:
a = nv.ArcPlot(G,node_order='degree',node_color='dc')
a.draw()

plt.show()


Betweenness Centrality

  • Bridges, gate keepers

In [204]:
betw_cent = nx.betweenness_centrality(G)
betw_cent


Out[204]:
{0: 0.0,
 1: 0.0,
 2: 0.0,
 3: 0.0,
 4: 0.0,
 5: 0.0,
 6: 0.0,
 7: 0.525,
 8: 0.5333333333333333,
 9: 0.525,
 10: 0.0,
 11: 0.0,
 12: 0.0,
 13: 0.0,
 14: 0.0,
 15: 0.0,
 16: 0.0}

In [205]:
for n in G.nodes():
    G.node[n]['bc'] = betw_cent[n]

In [206]:
a = nv.ArcPlot(G,node_order='degree',node_color='bc')
a.draw()

plt.show()


Closeness Centrality

  • close to all other nodes

In [207]:
close_cent = nx.closeness_centrality(G)
close_cent


Out[207]:
{0: 0.4,
 1: 0.4,
 2: 0.4,
 3: 0.4,
 4: 0.4,
 5: 0.4,
 6: 0.4,
 7: 0.5161290322580645,
 8: 0.5333333333333333,
 9: 0.5161290322580645,
 10: 0.4,
 11: 0.4,
 12: 0.4,
 13: 0.4,
 14: 0.4,
 15: 0.4,
 16: 0.4}

In [208]:
for n in G.nodes():
    G.node[n]['cc'] = close_cent[n]

In [209]:
a = nv.ArcPlot(G,node_order='degree',node_color='cc')
a.draw()

plt.show()


Eigenvector Centrality

  • Connected to highly connected nodes.

In [210]:
eig_cent = nx.eigenvector_centrality(G)
eig_cent


Out[210]:
{0: 0.24817512119322183,
 1: 0.24817512119322183,
 2: 0.24817512119322183,
 3: 0.24817512119322183,
 4: 0.24817512119322183,
 5: 0.24817512119322183,
 6: 0.24817512119322183,
 7: 0.2572744446955805,
 8: 0.07312488839906782,
 9: 0.2572744446955805,
 10: 0.24817512119322183,
 11: 0.24817512119322183,
 12: 0.24817512119322183,
 13: 0.24817512119322183,
 14: 0.24817512119322183,
 15: 0.24817512119322183,
 16: 0.24817512119322183}

In [211]:
for n in G.nodes():
    G.node[n]['ec'] = eig_cent[n]

In [212]:
a = nv.ArcPlot(G,node_order='degree',node_color='ec')
a.draw()

plt.show()


Cliques


In [213]:
cliques = list(nx.find_cliques(G))
cliques


Out[213]:
[[7, 0, 1, 2, 3, 4, 5, 6], [7, 8], [9, 8], [9, 10, 11, 12, 16, 13, 14, 15]]

In [214]:
nx.draw(G.subgraph(cliques[0]))
plt.show()



In [215]:
nx.draw(G.subgraph(cliques[-1]))
plt.show()


Example

  • using a random graph of 20 nodes w/ .2 chance of making an edge.

In [216]:
T = nx.erdos_renyi_graph(n=20, p=.2)

print('N','=',len(T.nodes()),'|','E','=',len(T.edges()))


N = 20 | E = 33

In [217]:
noc = nx.number_connected_components(G)
print('# of components',noc)


# of components 1

In [218]:
c = nv.CircosPlot(T)
c.draw()
plt.show()



In [219]:
cliques = sorted(nx.find_cliques(T), key = lambda x: len(x))
cliques


Out[219]:
[[0, 4],
 [0, 6],
 [0, 7],
 [0, 14],
 [0, 19],
 [1, 17],
 [1, 15],
 [2, 10],
 [3, 11],
 [3, 4],
 [5, 10],
 [7, 16],
 [11, 19],
 [11, 15],
 [13, 19],
 [13, 14],
 [14, 16],
 [14, 12],
 [15, 9],
 [15, 4],
 [16, 10],
 [2, 8, 9],
 [2, 8, 18],
 [2, 8, 4],
 [2, 12, 18],
 [16, 8, 19]]

In [220]:
largest_clique = cliques[-1]
largest_clique


Out[220]:
[16, 8, 19]
1. add in all the neighbors for the largest clique

In [221]:
sub_nodes = set(largest_clique)

for node in largest_clique:
    for neighbor in T.neighbors(node):
        sub_nodes.add(neighbor)

sub_nodes


Out[221]:
{0, 2, 4, 7, 8, 9, 10, 11, 13, 14, 16, 18, 19}
2. display the largest clique + neighbors subgraph

In [222]:
sub_graph = T.subgraph(sub_nodes)
nx.draw(sub_graph)
plt.show()


3. add deg to the nodes metadata

In [223]:
for node in sub_graph.nodes():
    sub_graph.node[node]['deg'] = sub_graph.degree(node)

a = nv.ArcPlot(sub_graph,node_order='deg')
a.draw()

plt.show()


4. build recommendations by finding all the neighbors of a node who are not yet connected.

In [224]:
from itertools import combinations
from collections import defaultdict

In [225]:
recommended = defaultdict(int)

for n,d in sub_graph.nodes(data=True):
    for n1,n2 in combinations(sub_graph.neighbors(n),2):
        if not sub_graph.has_edge(n1,n2):
            recommended[(n1, n2)] += 1

recommended


Out[225]:
defaultdict(int,
            {(0, 2): 1,
             (0, 8): 2,
             (0, 11): 1,
             (0, 13): 2,
             (0, 16): 3,
             (2, 16): 2,
             (2, 19): 1,
             (4, 7): 1,
             (4, 9): 2,
             (4, 10): 1,
             (4, 14): 1,
             (4, 16): 1,
             (4, 18): 2,
             (4, 19): 2,
             (7, 8): 1,
             (7, 10): 1,
             (7, 14): 2,
             (7, 19): 2,
             (8, 10): 2,
             (8, 11): 1,
             (8, 13): 1,
             (8, 14): 1,
             (9, 10): 1,
             (9, 16): 1,
             (9, 18): 2,
             (9, 19): 1,
             (10, 14): 1,
             (10, 18): 1,
             (10, 19): 1,
             (11, 13): 1,
             (11, 16): 1,
             (13, 16): 2,
             (14, 19): 3,
             (16, 18): 1,
             (18, 19): 1})

In [ ]: